package com.telecom.cms.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.telecom.cms.dto.SysTeam;
import com.telecom.cms.util.Constant;
import com.telecom.cms.util.CurrentTime;

public class SysTeamDAO {

	Dao dao = new DaoImp();

	String db = (new Constant()).getPara("DATABASE");

	public int insert(SysTeam transientInstance) {
		int rs = 0;
		String sql = "select * from  " + db + ".Sys_Team where Team_id="
				+ transientInstance.getTeamId();
		try {
			ResultSet rs2 = dao.select(sql);
			if (rs2.next())
				sql = "update " + db + ".Sys_Team set latn_id='"

				+ transientInstance.getLatnId() + "',team_name='"
						+ transientInstance.getTeamName() + "',leader_name='"
						+ transientInstance.getLeaderName()
						+ "',Creator_Name='"
						+ transientInstance.getCreatorName()
						+ "',Create_Time=timestamp'"
						+ CurrentTime.getCurrentTime() + "' where Team_id="
						+ transientInstance.getTeamId();
			else
				sql = "insert into " + db + ".Sys_Team values(,'"

				+ transientInstance.getLatnId() + "','"
						+ transientInstance.getTeamName() + "','"
						+ transientInstance.getLeaderName() + "','"
						+ transientInstance.getCreatorName() + "',timestamp'"
						+ CurrentTime.getCurrentTime() + "')";
			rs = dao.insert(sql);
			if (rs2 != null) {

				rs2.close();
			}
			
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}finally
		{
			dao.closeconn();
		}
		return rs;
	}

	public void delete(SysTeam persistentInstance) {

		String sql = "delete  from " + db + ".Sys_Team where Team_Id = "
				+ persistentInstance.getTeamId();

		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public SysTeam findById(int id) {

		String sql = "select * from  " + db + ".Sys_Team where Team_Id = " + id;
		SysTeam st = new SysTeam();
		try {
			ResultSet rs = dao.select(sql);
			if (rs.next()) {

				st.setTeamId(rs.getInt(1));
				st.setLatnId(rs.getString(2));
				st.setTeamName(rs.getString(3));
				st.setLeaderName(rs.getString(4));
				st.setCreatorName(rs.getString(5));
				st.setCreateTime(rs.getString(6));

			}
			if (rs != null) {

				rs.close();
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			dao.closeconn();
		}

		return st;

	}

	public List findAll() {

		String sql = "select * from  " + db + ".Sys_Team ";
		List list = new ArrayList();

		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {

				SysTeam st = new SysTeam();
				st.setTeamId(rs.getInt(1));
				st.setLatnId(rs.getString(2));
				st.setTeamName(rs.getString(3));
				st.setLeaderName(rs.getString(4));
				st.setCreatorName(rs.getString(5));
				st.setCreateTime(rs.getString(6));

				list.add(st);
			}
			if (rs != null) {

				rs.close();
			}
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			dao.closeconn();
		}
		return list;

	}
	
	public List findAll(String latnid) {

		String sql = "select * from  " + db + ".Sys_Team where latn_id='"+latnid+"'";
		List list = new ArrayList();

		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {

				SysTeam st = new SysTeam();
				st.setTeamId(rs.getInt(1));
				st.setLatnId(rs.getString(2));
				st.setTeamName(rs.getString(3));
				st.setLeaderName(rs.getString(4));
				st.setCreatorName(rs.getString(5));
				st.setCreateTime(rs.getString(6));

				list.add(st);
			}
			if (rs != null) {

				rs.close();
			}
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			dao.closeconn();
		}
		return list;

	}

}